﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using HMS_Hospital_Team2._6.GUI.Report;
using System.Collections;
using HMS_Hospital_Team2._6.DA;

namespace HMS_Hospital_Team2._6.GUI
{
    public partial class Form1 : Form
    {
        string mabn = "";
        public Form1(string MaBN)
        {
            InitializeComponent();
            mabn = MaBN;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BarcodeLib.Barcode bar = new BarcodeLib.Barcode();
            bar.Width = 150;
            bar.Height = 50;
            bar.IncludeLabel = true;
            bar.ForeColor = Color.Black;
            bar.BackColor = Color.White;
            Image barcodeImage = bar.Encode(BarcodeLib.TYPE.CODE128, mabn);
            barcodeImage.Save(Environment.CurrentDirectory + "\\Barcode.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
            
            
            
            CrystalReport1 cryRpt = new CrystalReport1();
            TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
            TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
            ConnectionInfo crConnectionInfo = new ConnectionInfo();
            Tables CrTables;
            HMSHospitalEntities context = new HMSHospitalEntities();
            var obj = context.Configs.FirstOrDefault();

            crConnectionInfo.ServerName = obj.Sername;
            crConnectionInfo.DatabaseName = obj.Dataname;
            crConnectionInfo.UserID = obj.username;
            crConnectionInfo.Password = obj.pass;

            CrTables = cryRpt.Database.Tables;
            foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
            {
                crtableLogoninfo = CrTable.LogOnInfo;
                crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crtableLogoninfo);
            }
            ParameterFieldDefinitions crParameterFieldDefinitions;
            ParameterFieldDefinition crParameterFieldDefinition;
            ParameterValues crParameterValues = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

            crParameterDiscreteValue.Value = mabn;
            crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions["Ma_BN"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;

            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);


            cryRpt.SetParameterValue(1, Environment.CurrentDirectory + "\\Barcode.jpeg");

            crystalReportViewer1.ReportSource = cryRpt;        
        }
    }
}
